<script>
	import {
		getToken,
		getTokenExpired,
		removeToken,
		removeTokenExpired
	} from './utils/localStoreToken.js'

	import {
		userStore
	} from './stores/userInfo/userInfo.js'
	import init from '@/common/init.js'
	export default {
		onLaunch: function() {
			init().then((res) => {
				if (res === 'ok') {
					// 白名单
					let white = ['/pages/user/forgetPassword/forgetPassword', '/pages/user/register/register']
					let changeArr = ['navigateTo', 'redirectTo', 'reLaunch']
					changeArr.forEach(v => {
						uni.addInterceptor(v, {
							invoke(args) {
								if (!args.url) {
									return false;
								}
								let token = getToken()
								let tokenExpired = getTokenExpired()
								let date = Date.now()
								if (token) {
									// token是否过期
									if (tokenExpired <= date) {
										uni.$u.toast('登录过期,请重新登录')
										removeToken()
										removeTokenExpired()
										args.url = '/pages/user/login/login'
									} else {
										return true
									}
								} else {
									let toUrl = args.url
									// 白名单
									if (white.includes(args.url) || args.url.startsWith(
											'/pages/goodsDetail/goodsDetail')) {
										return true
									}
									// 路径等于登录return 
									uni.$u.toast('请先登录')
									if (toUrl !== `/pages/user/login/login`) {
										args.url = `/pages/user/login/login?toUrl=${toUrl}`
									} else {
										args.url = `/pages/user/login/login`
									}
									return true
								}
							},
							success() {},
							fail(err) {},
							complete(res) {}
						})
					})
				}
			})

		},
		onShow: function() {

		},
		onHide: function() {
		}
	}
</script>

<style lang="scss">
	.container {
		min-height: calc(100vh - 85rpx);
		height: max-content;
		background-color: #f8f8f8;
	}

	@import "./uni_modules/vk-uview-ui/index.scss";
	/*每个页面公共css */
	@import "./static/iconfont/iconfont.css";
</style>
